//
//  AppDelegate.swift
//  TodayNews
//
//  Created by monkey on 2020/2/2.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import Kingfisher
import KingfisherWebP
import SwiftTheme
import LeanCloud

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
    {
        // 初始化设置
        initializeSetting()
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = UIColor.white
        window?.rootViewController = rootViewController
            //UINavigationController(rootViewController: XGLoginViewController())
        window?.makeKeyAndVisible()
        UIApplication.shared.isStatusBarHidden = false
//        UINavigationController.awake()
        // U-Share 平台设置
        configUSharePlatforms()
        return true
    }
    
    func applicationDidReceiveMemoryWarning(_ application: UIApplication)
    {
        KingfisherManager.shared.cache.clearMemoryCache()
    }
    
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool
    {
        let result = UMSocialManager.default()?.handleOpen(url, sourceApplication: sourceApplication, annotation: annotation) ?? false
        if !result {
            // 其他如支付等SDK的回调
        }
        return result
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
    {
        let result = UMSocialManager.default()?.handleOpen(url, options: options) ?? false
        if !result {
            // 其他如支付等SDK的回调
        }
        return result
    }
}

private extension AppDelegate
{
    var rootViewController: UIViewController {
        if XGUserAccountManager.shared.isLogin {
            return XGMainViewController()
        } else {
            return UINavigationController(rootViewController: XGLoginViewController())
        }
    }
    
    /// 初始化设置
    func initializeSetting() -> Void
    {
        // 设置Kingfisher支持webP
        KingfisherManager.shared.defaultOptions += [
          .processor(WebPProcessor.default),
          .cacheSerializer(WebPSerializer.default)
        ]
        
        // 初始化主题管理
        _ = TodayNewsThemeManager.shared
        
        // 设置状态栏样式
        UIApplication.shared.theme_setStatusBarStyle("styles.status_bar", animated: true)
        
        // 通知监听
        NotificationCenter.default.addObserver(self, selector: #selector(switchKeyWindowRootViewControllerNotification(noti:)), name: kSwitchKeyWindowRootViewControllerNotification, object: nil)
        
        // 初始化LeanCould
        do {
            try LCApplication.default.set(
                id: "a3xjglPCaWFmQ4bGlwhIHrxr-gzGzoHsz",
                key: "tOEa2HfpywFkvE2LLQPQtsc7",
                serverURL: "https://a3xjglpc.lc-cn-n1-shared.com")
        } catch {
            print(error)
        }
        // 在 Application 初始化代码执行之前执行
        LCApplication.logLevel = .all
        XGUserModel.register()
    }
    
    /// 切换根控制器通知监听方法
    @objc func switchKeyWindowRootViewControllerNotification(noti: Notification) -> Void
    {
        if let loginType = noti.object as? XGLoginViewController.Type {
            window?.rootViewController = UINavigationController(rootViewController: loginType.init())
        } else if let mainType = noti.object as? XGMainViewController.Type {
            window?.rootViewController = mainType.init()
        }
    }
        
    func configUSharePlatforms() -> Void
    {
        UMConfigure.initWithAppkey("59892ebcaed179694b000104", channel: "App Store")
        
        // 设置微信的appKey和appSecret
        UMSocialManager.default()?.setPlaform(.wechatSession, appKey: "wxdc1e388c3822c80b", appSecret: "3baf1193c85774b3fd9d18447d76cab0", redirectURL: "http://mobile.umeng.com/social")
        // qq
        UMSocialManager.default()?.setPlaform(.QQ, appKey: "1105821097", appSecret: "", redirectURL: "http://mobile.umeng.com/social")
        // sina
        UMSocialManager.default()?.setPlaform(.sina, appKey: "3368233039", appSecret: "90ed05b024368de6d789cd4569d15ec9", redirectURL: "https://api.weibo.com/oauth2/default.html")
    }
}
